﻿using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.Office.Interop.Excel;

namespace YArchitech.HVAC
{
	internal class WinFormTools
	{
		public static bool IsHaveUseless(string txt)
		{
			char[] array = new char[]
			{
				'~',
				'`',
				'!',
				'@',
				'#',
				'$',
				'%',
				'^',
				'&',
				'*',
				'(',
				')',
				'<',
				'>',
				'?',
				':',
				'"',
				'{',
				'}'
			};
			char[] array2 = txt.Trim().ToCharArray();
			int num = 0;
			for (int i = 0; i < array.Length; i++)
			{
				for (int j = 0; j < array2.Length; j++)
				{
					if (array[i] == array2[j])
					{
						num++;
					}
				}
			}
			return num > 0;
		}

		public static bool IsHaveUselessReg(string txt)
		{
			char[] array = new char[]
			{
				'~',
				'`',
				'!',
				'@',
				'#',
				'$',
				'%',
				'^',
				'&',
				'(',
				')',
				'<',
				'>',
				':',
				'"',
				'{',
				'}'
			};
			char[] array2 = txt.Trim().ToCharArray();
			int num = 0;
			for (int i = 0; i < array.Length; i++)
			{
				for (int j = 0; j < array2.Length; j++)
				{
					if (array[i] == array2[j])
					{
						num++;
					}
				}
			}
			return num > 0;
		}

		public static string hyGetSpaceTypesPath()
		{
			string text = Product.UserDataLocation + "\\SpaceTypes.xml";
			if (!File.Exists(text))
			{
				File.Copy(WinFormTools.hyDataPath() + "SpaceTypes.xml", text);
			}
			return text;
		}

		public static string hyGetSpaceKeywordPath()
		{
			string text = Product.UserDataLocation + "\\SpaceKeyword.xml";
			if (!File.Exists(text))
			{
				File.Copy(WinFormTools.hyDataPath() + "SpaceKeyword.xml", text);
			}
			return text;
		}

		public static string hyGetPath()
		{
			return Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Help\\HYRME.chm";
		}

		public static void DisThread(Thread thread)
		{
			if (thread != null)
			{
				thread.Abort();
				thread.IsBackground = true;
				thread = null;
			}
		}

		public static Thread ThreadShowProgressBar(ExternalCommandData commandData)
		{
			return null;
		}

		public static void Method3D()
		{
			WinFormTools.proBar.ShowDialog(WinFormTools.GetRevitForm());
		}

		public static void ThreadMethod3D()
		{
			WinFormTools.proBar.ShowDialog(WinFormTools.GetRevitForm());
		}

		public static void ChangeTreeViewColor(TreeView treeView)
		{
			treeView.DrawMode = TreeViewDrawMode.OwnerDrawText;
			treeView.DrawNode += WinFormTools.treeView_DrawNode;
		}

		public static void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			e.DrawDefault = true;
		}

		public static IntPtr GetIntPtr()
		{
			return Process.GetCurrentProcess().MainWindowHandle;
		}

		public static Form GetRevitForm()
		{
			return (Form)Control.FromHandle(WinFormTools.GetIntPtr());
		}

		public static void DeleteExcel()
		{
			File.Delete("");
		}

		public static void InitExcel()
		{
			string filename = WinFormTools.hyDataPath() + "SpaceKeyword.xlsx";
			WinFormTools.app = (Microsoft.Office.Interop.Excel.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
			WinFormTools.wb = WinFormTools.app.Workbooks.Open(filename, false, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing, WinFormTools.missing);
			WinFormTools.app.Visible = false;
            //if (WinFormTools.<>o__21.<>p__0 == null)
            //{
            //    WinFormTools.<>o__21.<>p__0 = CallSite<Func<CallSite, object, Worksheet>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(Worksheet), typeof(WinFormTools)));
            //}
            //WinFormTools.ws = WinFormTools.<>o__21.<>p__0.Target(WinFormTools.<>o__21.<>p__0, WinFormTools.wb.Worksheets.get_Item(1));
		}

		public static void ReadExcel()
		{
			WinFormTools.InitExcel();
			WinFormTools.r = WinFormTools.ws.get_Range("A1", Type.Missing);
            //if (WinFormTools.<>o__22.<>p__0 == null)
            //{
            //    WinFormTools.<>o__22.<>p__0 = CallSite<Func<CallSite, object, string>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(WinFormTools)));
            //}
            //string text = WinFormTools.<>o__22.<>p__0.Target(WinFormTools.<>o__22.<>p__0, WinFormTools.r.get_Value(Type.Missing));
			WinFormTools.app.Quit();
            //MessageBox.Show(text);
		}

		public static void WriteExcel()
		{
			WinFormTools.InitExcel();
			WinFormTools.ws.Cells[1, 1] = "修改的内容";
			WinFormTools.wb.SaveAs("D:\\hi.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
			WinFormTools.wb.Close(null, null, null);
			WinFormTools.app.Workbooks.Close();
			WinFormTools.app.Application.Quit();
			WinFormTools.app.Quit();
			Marshal.ReleaseComObject(WinFormTools.ws);
			Marshal.ReleaseComObject(WinFormTools.wb);
			Marshal.ReleaseComObject(WinFormTools.app);
			WinFormTools.ws = null;
			WinFormTools.wb = null;
			WinFormTools.app = null;
		}

		public static string hyDataPath()
		{
			return Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Data\\";
		}

		public static void BindComboBox(System.Windows.Forms.ComboBox comBox, XmlNode xmlNode)
		{
			comBox.Items.Clear();
			string value = xmlNode.Attributes["Type"].Value;
			if (value == "Double")
			{
				WinFormTools.BindGreatLess(comBox);
				return;
			}
			if (value == "Integer")
			{
				WinFormTools.BindGreatLess(comBox);
				WinFormTools.BindEqualNot(comBox);
				return;
			}
			if (value == "String")
			{
				WinFormTools.BindEqualNot(comBox);
				return;
			}
			if (!(value == "ElementId"))
			{
				return;
			}
			WinFormTools.BindEqualNot(comBox);
		}

		public static void BindValueComboBox(System.Windows.Forms.ComboBox comBox, XmlNodeList xmlNodeList)
		{
			foreach (object obj in xmlNodeList)
			{
				XmlNode xmlNode = (XmlNode)obj;
				comBox.Items.Add(xmlNode.InnerText);
			}
		}

		public static void BindGreatLess(System.Windows.Forms.ComboBox comboBox)
		{
			comboBox.Items.Add("大于");
			comboBox.Items.Add("小于");
		}

		public static void BindEqualNot(System.Windows.Forms.ComboBox comboBox)
		{
			comboBox.Items.Add("等于");
			comboBox.Items.Add("不等于");
		}

		public static void MsgBox(string str)
		{
		}

		public static void SelectTreeView(TreeView treeView, string selectStr)
		{
			treeView.Focus();
			for (int i = 0; i < treeView.Nodes.Count; i++)
			{
				for (int j = 0; j < treeView.Nodes[i].Nodes.Count; j++)
				{
					if (treeView.Nodes[i].Nodes[j].Text == selectStr)
					{
						treeView.SelectedNode = treeView.Nodes[i].Nodes[j];
						treeView.Nodes[i].Expand();
						return;
					}
				}
			}
		}

		public static void SelectTreeView3Level(TreeView treeView, string selectStr)
		{
			treeView.Focus();
			for (int i = 0; i < treeView.Nodes.Count; i++)
			{
				for (int j = 0; j < treeView.Nodes[i].Nodes.Count; j++)
				{
					for (int k = 0; k < treeView.Nodes[i].Nodes[j].Nodes.Count; k++)
					{
						if (treeView.Nodes[i].Nodes[j].Nodes[k].Text == selectStr)
						{
							treeView.SelectedNode = treeView.Nodes[i].Nodes[j].Nodes[k];
							treeView.Nodes[i].Expand();
							treeView.Nodes[i].Nodes[j].Expand();
							return;
						}
					}
				}
			}
		}

		public static bool CheckedListBoxIsCheck(CheckedListBox checkedListBox)
		{
			bool result = false;
			for (int i = 0; i < checkedListBox.Items.Count; i++)
			{
				if (checkedListBox.GetItemChecked(i))
				{
					result = true;
				}
			}
			return result;
		}

		public static bool IsDefault(string strName)
		{
			return new ArrayList
			{
				"风管",
				"风系统",
				"水管",
				"水系统",
				"建筑"
			}.Contains(strName);
		}

		public static bool IsAdministrator()
		{
			return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
		}

		[DllImport("user32.dll")]
		public static extern IntPtr GetForegroundWindow();

		public static void SetPanelColor(Panel panel, PaintEventArgs e)
		{
			ControlPaint.DrawBorder(e.Graphics, panel.ClientRectangle, Color.LightSeaGreen, 1, ButtonBorderStyle.Solid, Color.LightSeaGreen, 1, ButtonBorderStyle.Solid, Color.LightSeaGreen, 1, ButtonBorderStyle.Solid, Color.LightSeaGreen, 1, ButtonBorderStyle.Solid);
		}

		public static hyFrmManage frmSelectSet = null;

		public static HYProgressBar proBar = new HYProgressBar();

		public static object missing = Missing.Value;

		public static Microsoft.Office.Interop.Excel.Application app = null;

		public static Workbook wb = null;

		public static Worksheet ws = null;

		public static Range r = null;
	}
}
